#!/bin/bash
# Script to instantiate chaincode
cp $FABRIC_CFG_PATH/core.yaml /vars/core.yaml
cd /vars
export FABRIC_CFG_PATH=/vars

{% if CHANNEL_NAME == 'systemchannel' %}
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_TLS_ROOTCERT_FILE=/vars/keyfiles/ordererOrganizations/{{ actingorderer.org }}/orderers/{{ actingorderer.fullname }}/tls/ca.crt
export CORE_PEER_LOCALMSPID={{ actingorderer.org.split('.')|join('-') }}
export CORE_PEER_MSPCONFIGPATH=/vars/keyfiles/ordererOrganizations/{{ actingorderer.org }}/users/Admin@{{ actingorderer.org }}/msp
export ORDERER_TLS_CA=/vars/keyfiles/ordererOrganizations/{{ actingorderer.org}}/orderers/{{ actingorderer.fullname }}/tls/ca.crt
export ORDERER_ADDRESS={{ actingorderer.url }}:{{ actingorderer.port }}
{% else %}
{% include "envsetting.j2" %}
{% endif %}

peer channel fetch config config_block.pb -o $ORDERER_ADDRESS \
  --cafile $ORDERER_TLS_CA --tls -c {{CHANNEL_NAME}}

configtxlator proto_decode --input config_block.pb --type common.Block \
  | jq .data.data[0].payload.data.config > {{ CHANNEL_NAME }}_config.json